// 
// SMS Notifier
// 
// By Aali Alikoski (aali@microsoft.com, http://blogs.msdn.com/aalialikoski)
// Originally developed as a sample for demonstrating Windows Mobile 5 managed APIs
// at Tampere University of Technology mobile development seminar
//

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsMobile.Status;
using SMSNotifierCore;

namespace SMSNotifier_WM6_Pro
{
    public partial class Form1 : Form
    {
        SystemState missedCalls = new SystemState(SystemProperty.PhoneMissedCall);
        Boolean bInitializingControlValues = false;

        SMSNotifierCore.CoreLogic _logic = new CoreLogic();
        
        public Form1()
        {
            InitializeComponent();
        }

        private void chkAppendNextFreeSlot_CheckStateChanged(object sender, EventArgs e)
        {
            txtFreeSlotText.Enabled = chkAppendNextFreeSlot.Checked;
            ControlvalueChanged(null, null);
        }

        private void chkSendSMS_CheckStateChanged(object sender, EventArgs e)
        {
            txtMessageToSend.Enabled = chkSendSMS.Checked;
            chkIfActiveAppointment.Enabled = chkSendSMS.Checked;
            chkIfBusyAppointment.Enabled = chkSendSMS.Checked;
            chkAppendNextFreeSlot.Enabled = chkSendSMS.Checked;
            txtFreeSlotText.Enabled = chkSendSMS.Checked;
            chkAddMissedCallToCalendar.Enabled = chkSendSMS.Checked;
            ControlvalueChanged(null, null);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            bInitializingControlValues = true;
            _BindSettingsToControlvalues(_logic.Config);
            bInitializingControlValues = false;
            missedCalls.Changed += new ChangeEventHandler(_logic.missedCalls_Changed);
            lblVersion.Text = "Version: " + GetVersionString();
        }

        private String GetVersionString()
        {
            Version v = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
            return v.Major + "." + v.Minor; // Omit the rest for now:  +"." + v.Revision + "." + v.Build;
        }

        private void chkIfActiveAppointment_CheckStateChanged(object sender, EventArgs e)
        {
            chkIfBusyAppointment.Enabled = chkIfActiveAppointment.Checked;
            ControlvalueChanged(null, null);
        }

        private void _BindControlvaluesToSettings(SMSsettings s)
        {
            s.AddMissedCallInfoToCalendar = chkAddMissedCallToCalendar.Checked;
            s.AppendNextFreeSlotMessage = chkAppendNextFreeSlot.Checked;
            s.FreeSlotTextFormat = txtFreeSlotText.Text;
            s.SendSMSOnlyWhenActiveAppointment = chkIfActiveAppointment.Checked;
            s.SendSMSOnlyWhenBusyAppointment = chkIfBusyAppointment.Checked;
            s.SendSMSToUnansweredCallers = chkSendSMS.Checked;
            s.SMSMessageToSend = txtMessageToSend.Text;
            s.Save();
        }

        private void _BindSettingsToControlvalues(SMSsettings s)
        {
            chkAddMissedCallToCalendar.Checked = s.AddMissedCallInfoToCalendar;
            chkAppendNextFreeSlot.Checked = s.AppendNextFreeSlotMessage;
            txtFreeSlotText.Text = s.FreeSlotTextFormat;
            chkIfActiveAppointment.Checked = s.SendSMSOnlyWhenActiveAppointment;
            chkIfBusyAppointment.Checked = s.SendSMSOnlyWhenBusyAppointment;
            chkSendSMS.Checked = s.SendSMSToUnansweredCallers;
            txtMessageToSend.Text = s.SMSMessageToSend;
        }

        private void ControlvalueChanged(object sender, EventArgs e)
        {
            if (!bInitializingControlValues)
                _BindControlvaluesToSettings(_logic.Config);
        }

        private void linkReleases_Click(object sender, EventArgs e)
        {
            _logic.OpenProjectReleases();
        }

        private void linkHomepage_Click(object sender, EventArgs e)
        {
            _logic.OpenProjectHomepage();
        }
    }
}